package echat.future.cn.echat.home.network;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;

import com.zhusx.core.app._BaseAppCompatActivity;
import com.zhusx.core.app._BaseFragment;
import com.zhusx.core.debug.LogUtil;
import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.network._BaseRequestData;
import com.zhusx.core.utils._Toast;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import echat.future.cn.echat.R;
import echat.future.cn.echat.home.bean.JSONResult;
import echat.future.cn.echat.my.utils.UserHelper;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;

/**
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/8/23 9:15
 */
public class LoadData<T> extends _BaseRequestData<LoadData.Api, T, Object, JSONResult<T>> {
    private CompositeDisposable mCompositeSubscription;
    private Context context;

    public LoadData(Api api, _BaseFragment cycle) {
        super(api, cycle);
        context = cycle.getActivity();
        this.mCompositeSubscription = new CompositeDisposable();
    }

    public LoadData(Api api, _BaseAppCompatActivity cycle) {
        super(api, cycle);
        context = cycle;
        this.mCompositeSubscription = new CompositeDisposable();
    }

    protected Observable<JSONResult<T>> getHttpParams(Api api, Object[] o) {
        switch (api) {
            case banner:
                return cast(Retrofits.Companion.createApi(ApiService.class).getBanner());
            case homePage:
                String gender = "";
                String gj = "";
                switch (valueOf(o[0])) {
                    case "gender":
                        gender = valueOf(o[1]);
                        break;
                    case "star":
                        gj = valueOf(o[1]);
                        break;
                }
                return cast(Retrofits.Companion.createApi(ApiService.class).homePage(UserHelper.getToken(), valueOf(o[0]), gj, _getNextPage()));
            case 用户亲密榜:
                return cast(Retrofits.Companion.createApi(ApiService.class).qinmibang(UserHelper.getToken(), valueOf(o[0])));
            case 用户礼物柜:
                return cast(Retrofits.Companion.createApi(ApiService.class).liwugui(UserHelper.getToken(), valueOf(o[0])));
            case 某个主播详情:
                return cast(Retrofits.Companion.createApi(ApiService.class).get_one_anchor(UserHelper.getToken(), valueOf(o[0])));
            case 主播的小视频:
                return cast(Retrofits.Companion.createApi(ApiService.class).my_video(UserHelper.getToken(), valueOf(o[0])));
            case 关注主播:
                return cast(Retrofits.Companion.createApi(ApiService.class).openfans(UserHelper.getToken(), valueOf(o[0]), valueOf(o[1])));
            case 形象标签列表:
                return cast(Retrofits.Companion.createApi(ApiService.class).auth_sign(UserHelper.getToken()));
            case 搜索:
                return cast(Retrofits.Companion.createApi(ApiService.class).search(valueOf(o[0])));
            case 观看付费视频:
                return cast(Retrofits.Companion.createApi(ApiService.class).pay_video(UserHelper.getToken(), valueOf(o[0]), valueOf(o[1])));
        }
        return null;
    }

    public enum Api {
        banner, homePage, 用户礼物柜, 用户亲密榜, 某个主播详情, 主播的小视频, 关注主播, 形象标签列表, 搜索, 观看付费视频
    }

    @Override
    protected void __requestProtocol(Api api, Object... objects) {
        _onStart();
        Observable<JSONResult<T>> observable = getHttpParams(api, objects);
        DisposableObserver observer = new DisposableObserver<JSONResult<T>>() {

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
                String errorMessage = "未知异常";
                if (e instanceof HttpException) {
                    if (((HttpException) e).code() == 404) {
                        errorMessage = "找不到网络";
                    } else if (((HttpException) e).code() > 500) {
                        errorMessage = "服务器异常";
                    } else {
                        errorMessage = "系统异常";
                    }
                }
                if (e instanceof ConnectException || e instanceof SocketTimeoutException) {
                    errorMessage = "网络链接异常";
                } else if (e instanceof JSONException || e instanceof ParseException) {
                    errorMessage = "数据解析错误";
                } else if (e instanceof UnknownHostException) {
                    errorMessage = "找不到域名";
                }
                _onError(errorMessage);
            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onNext(JSONResult<T> data) {
                if (data.isSuccess()) {
                    _onComplete(data);
                } else {
                    _onError(data.getMessage());
                }
            }
        };
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
        this.mCompositeSubscription.add(observer);
    }

    @Override
    protected void __onStart(Api api, HttpRequest<Object> httpRequest) {
        switch (api) {
            case 关注主播:
            case 观看付费视频:
            case 某个主播详情:
            case 搜索:
                showProgress();
                break;
        }
    }

    @Override
    protected void __onError(Api api, HttpRequest<Object> httpRequest, IHttpResult<T> iHttpResult, boolean b, String s) {
        switch (api){
            case 关注主播:
            case 观看付费视频:
                _Toast._show(s);
                break;
        }
        dismiss();
    }

    @Override
    protected void __onComplete(Api api, HttpRequest<Object> httpRequest, IHttpResult<T> iHttpResult) {
        dismiss();
    }

    @Override
    public void _cancelLoadData() {
        super._cancelLoadData();
        if (mCompositeSubscription != null && mCompositeSubscription.isDisposed()) {
            mCompositeSubscription.dispose();
            mCompositeSubscription = null;
        }
    }

    Dialog progressDialog;

    public void showProgress() {
        if (progressDialog == null) {
            progressDialog = new Dialog(context, R.style.BMF_Dialog);
            progressDialog.setContentView(R.layout.common_progress_bar);
        }
        progressDialog.show();
    }

    public void dismiss() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
